home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection Student Program / ADC Tools Sampler CD Disk 3 1999.iso / Metrowerks CodeWarrior / Java Support / Java_Source / Java2 / src / java / io / StringReader.java < prev    next >
Encoding:
Java Source  |  1999-05-28  |  4.5 KB  |  175 lines  |  [TEXT/CWIE]

  1. /*
  2.  * @(#)StringReader.java    1.12 98/07/08
  3.  *
  4.  * Copyright 1996-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  *
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.io;
  16.  
  17.  
  18. /**
  19.  * A character stream whose source is a string.
  20.  *
  21.  * @version     1.12, 98/07/08
  22.  * @author    Mark Reinhold
  23.  * @since    JDK1.1
  24.  */
  25.  
  26. public class StringReader extends Reader {
  27.  
  28.     private String str;
  29.     private int length;
  30.     private int next = 0;
  31.     private int mark = 0;
  32.  
  33.     /**
  34.      * Create a new string reader.
  35.      */
  36.     public StringReader(String s) {
  37.     this.str = s;
  38.     this.length = s.length();
  39.     }
  40.  
  41.     /** Check to make sure that the stream has not been closed */
  42.     private void ensureOpen() throws IOException {
  43.     if (str == null)
  44.         throw new IOException("Stream closed");
  45.     }
  46.  
  47.     /**
  48.      * Read a single character.
  49.      *
  50.      * @return     The character read, or -1 if the end of the stream has been
  51.      *             reached
  52.      *
  53.      * @exception  IOException  If an I/O error occurs
  54.      */
  55.     public int read() throws IOException {
  56.     synchronized (lock) {
  57.         ensureOpen();
  58.         if (next >= length)
  59.         return -1;
  60.         return str.charAt(next++);
  61.     }
  62.     }
  63.  
  64.     /**
  65.      * Read characters into a portion of an array.
  66.      *
  67.      * @param      cbuf  Destination buffer
  68.      * @param      off   Offset at which to start writing characters
  69.      * @param      len   Maximum number of characters to read
  70.      *
  71.      * @return     The number of characters read, or -1 if the end of the
  72.      *             stream has been reached
  73.      *
  74.      * @exception  IOException  If an I/O error occurs
  75.      */
  76.     public int read(char cbuf[], int off, int len) throws IOException {
  77.     synchronized (lock) {
  78.         ensureOpen();
  79.             if ((off < 0) || (off > cbuf.length) || (len < 0) ||
  80.                 ((off + len) > cbuf.length) || ((off + len) < 0)) {
  81.                 throw new IndexOutOfBoundsException();
  82.             } else if (len == 0) {
  83.                 return 0;
  84.             }
  85.         if (next >= length)
  86.         return -1;
  87.         int n = Math.min(length - next, len);
  88.         str.getChars(next, next + n, cbuf, off);
  89.         next += n;
  90.         return n;
  91.     }
  92.     }
  93.  
  94.     /**
  95.      * Skip characters.
  96.      *
  97.      * @exception  IOException  If an I/O error occurs
  98.      */
  99.     public long skip(long ns) throws IOException {
  100.     synchronized (lock) {
  101.         ensureOpen();
  102.         if (next >= length)
  103.         return 0;
  104.         long n = Math.min(length - next, ns);
  105.         next += n;
  106.         return n;
  107.     }
  108.     }
  109.  
  110.     /**
  111.      * Tell whether this stream is ready to be read.
  112.      *
  113.      * @return True if the next read() is guaranteed not to block for input
  114.      *
  115.      * @exception  IOException  If the stream is closed
  116.      */
  117.     public boolean ready() throws IOException {
  118.         synchronized (lock) {
  119.         ensureOpen();
  120.         return true;
  121.         }
  122.     }
  123.  
  124.     /**
  125.      * Tell whether this stream supports the mark() operation, which it does.
  126.      */
  127.     public boolean markSupported() {
  128.     return true;
  129.     }
  130.  
  131.     /**
  132.      * Mark the present position in the stream.  Subsequent calls to reset()
  133.      * will reposition the stream to this point.
  134.      *
  135.      * @param  readAheadLimit  Limit on the number of characters that may be
  136.      *                         read while still preserving the mark.  Because
  137.      *                         the stream's input comes from a string, there
  138.      *                         is no actual limit, so this argument must not
  139.      *                         be negative, but is otherwise ignored.
  140.      *
  141.      * @exception  IllegalArgumentException  If readAheadLimit is < 0
  142.      * @exception  IOException  If an I/O error occurs
  143.      */
  144.     public void mark(int readAheadLimit) throws IOException {
  145.     if (readAheadLimit < 0){
  146.         throw new IllegalArgumentException("Read-ahead limit < 0");
  147.     }
  148.     synchronized (lock) {
  149.         ensureOpen();
  150.         mark = next;
  151.     }
  152.     }
  153.  
  154.     /**
  155.      * Reset the stream to the most recent mark, or to the beginning of the
  156.      * string if it has never been marked.
  157.      *
  158.      * @exception  IOException  If an I/O error occurs
  159.      */
  160.     public void reset() throws IOException {
  161.     synchronized (lock) {
  162.         ensureOpen();
  163.         next = mark;
  164.     }
  165.     }
  166.  
  167.     /**
  168.      * Close the stream.
  169.      */
  170.     public void close() {
  171.     str = null;
  172.     }
  173.  
  174. }
  175.